package com.ahwei.util;

import lombok.extern.slf4j.Slf4j;

/**
 * Log 日志记录工具类
 *
 * @author xujunwei
 * @version 1.0
 * @date createTime 2023/07/26
 */
@Slf4j
public class LogUtils {

    /**
     * 获取调用类名
     *
     * @return ·
     */
    public static String getCallingClassName() {
        String callingClassName = "";
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

        if (stackTrace.length >= 4) {
            StackTraceElement caller = stackTrace[3];
            callingClassName = "(" + caller.getFileName() + ":" + caller.getLineNumber() + ") : ";
        }
        return callingClassName;
    }

    /**
     * Log a message at the INFO level.
     *
     * @param msg the message string to be logged
     */
    public static void info(String msg) {
        log.info(getCallingClassName() + msg);
    }

    /**
     * Log a message at the INFO level according to the specified format
     * and arguments.
     * <p/>
     * <p>This form avoids superfluous string concatenation when the logger
     * is disabled for the INFO level. However, this variant incurs the hidden
     * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method,
     * even if this logger is disabled for INFO. The variants taking
     * arguments exist solely in order to avoid this hidden cost.</p>
     *
     * @param format    the format string
     * @param arguments a list of 3 or more arguments
     */
    public static void info(String format, Object... arguments) {
        log.info(getCallingClassName() + format, arguments);
    }

    /**
     * Log a message at the INFO level.
     *
     * @param msg the message string to be logged
     */
    public static void error(String msg) {
        log.error(getCallingClassName() + msg);
    }

    /**
     * Log a message at the ERROR level according to the specified format
     * and arguments.
     * <p/>
     * <p>This form avoids superfluous string concatenation when the logger
     * is disabled for the ERROR level. However, this variant incurs the hidden
     * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method,
     * even if this logger is disabled for ERROR. The variants taking
     * arguments exist solely in order to avoid this hidden cost.</p>
     *
     * @param format    the format string
     * @param arguments a list of 3 or more arguments
     */
    public static void error(String format, Object... arguments) {
        log.error(getCallingClassName() + format, arguments);
    }

}
